//
//  HMSportControlController.m
//  HMSport
//
//  Created by apple on 2017/4/21.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "HMSportControlController.h"
#import "HMSportTrackController.h"
#import "HMAnimator.h"
#import "UIColor+CZAddition.h"
#import "HMSportCameraController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface HMSportControlController ()<MPMediaPickerControllerDelegate>

//轨迹控制器
@property (nonatomic, strong) HMSportTrackController *trackVc;
//轨迹按钮
@property (weak, nonatomic) IBOutlet UIButton *trackBtn;
//GPS状态按钮
@property (weak, nonatomic) IBOutlet UIButton *gpsBtn;

@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *avgSpeedLabel;
//继续按钮x方向约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continueXConstraint;
//结束按钮x方向约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishXConstraint;
@property (weak, nonatomic) IBOutlet UIButton *pauseBtn;
@property (weak, nonatomic) IBOutlet UIButton *continueBtn;
@property (weak, nonatomic) IBOutlet UIButton *finishBtn;
//记录是否正在执行运动状态的操作
@property (nonatomic, assign) BOOL isAnimating;

@end

@implementation HMSportControlController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //一开始运动,就应该创建trackVc和mapVc,并且保证这里两个控制器不被释放
    self.trackVc = [[HMSportTrackController alloc] init];
    //传递轨迹模型
    self.trackVc.track = self.track;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationDidUpdateNote:) name:@"HMSportLocationDidUpdateNote" object:nil];
    

}


- (void)locationDidUpdateNote:(NSNotification *)note{
    //设置数据
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f", self.track.totalDistance];
    self.timeLabel.text = self.track.timeStr;
    self.avgSpeedLabel.text = [NSString stringWithFormat:@"%.2f", self.track.avgSpeed];
    
    if (!self.isAnimating) { //当该用户没有进行主动操作时,执行自动处理
        
        //获取当前的位置对象
        CLLocation *location = note.userInfo[@"HMSportLocationDidUpdateNoteLocationKey"];
        //判断速度
        if (location.speed == 0 && self.track.sportState == HMSportStateContinue) { //速度为0且当前状态为继续时,执行自动暂停处理
            
            [self clickSportStateBtn:self.pauseBtn];
            
        } else if (location.speed > 0 && self.track.sportState == HMSportStatePause) { //速度大于0并且当前状态为暂停时,执行自动继续
            
            [self clickSportStateBtn:self.continueBtn];
        }
        
    }
    
}


- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];
    //在界面间传递子视图尺寸时,注意在子视图布局完成后进行,避免子视图没有布局完成时,没有根据真实设备的尺寸进行处理
    //传递轨迹按钮中心点
    self.trackVc.trackBtnCenter = self.trackBtn.center;
    //添加背景图层
    [self addBackgroundLayer];
}


//添加背景渐变图层
- (void)addBackgroundLayer{
    //创建渐变图层
    CAGradientLayer *layer = [CAGradientLayer layer];
    //设置尺寸
    layer.bounds = self.view.bounds;
    //设置位置
    layer.position = self.view.center;
    //获取16进制颜色
    CGColorRef color1 = [UIColor cz_colorWithHex:0x0e1428].CGColor;
    CGColorRef color2 = [UIColor cz_colorWithHex:0x406479].CGColor;
    CGColorRef color3 = [UIColor cz_colorWithHex:0x406578].CGColor;
    //设置颜色数组
    layer.colors = @[(__bridge id)color1, (__bridge id)color2, (__bridge id)color3];
    //设置位置
    layer.locations = @[@0, @(0.6), @1];
    //添加图层
    [self.view.layer insertSublayer:layer atIndex:0];
}

#pragma mark - 响应事件

//点击播放音乐
- (IBAction)clickSettingsBtn:(id)sender {
    
    //打开媒体库  注意设置info.plist请求授权
    //创建控制器 设置音乐类型
    MPMediaPickerController *pickerVc = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    //设置属性
    pickerVc.delegate = self;
    //modal展示
    [self presentViewController:pickerVc animated:YES completion:nil];
}


//点击相机按钮
- (IBAction)clickCameraBtn:(id)sender {
    HMSportCameraController *cameraVc = [[HMSportCameraController alloc] init];
    //传递轨迹模型
    cameraVc.track = self.track;
    
    [self presentViewController:cameraVc animated:YES completion:nil];
}


//点击切换运动状态
- (IBAction)clickSportStateBtn:(UIButton *)sender {
    //用户开始操作后,停止自动处理
    self.isAnimating = YES;
    
    //改变运动状态
    self.track.sportState = sender.tag;
    
    switch (sender.tag) {
        case HMSportStatePause:
        {
            //继续按钮左偏
            self.continueXConstraint.constant -= 90;
            //结束按钮右偏
            self.finishXConstraint.constant += 90;
            //隐藏暂停按钮
            self.pauseBtn.hidden = YES;
            //在到达指定位置前,禁用继续&停止
            self.continueBtn.enabled = NO;
            self.finishBtn.enabled = NO;
            
            [UIView animateWithDuration:1.5 animations:^{
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
                
                //用户操作的动画执行完,恢复自动处理
                self.isAnimating = NO;
                self.continueBtn.enabled = YES;
                self.finishBtn.enabled = YES;
            }];
        }
            break;
        case HMSportStateContinue:
        {
            //继续按钮右偏
            self.continueXConstraint.constant += 90;
            //结束按钮左偏
            self.finishXConstraint.constant -= 90;
            //在到达指定位置前,禁用继续&停止
            self.continueBtn.enabled = NO;
            self.finishBtn.enabled = NO;
            
            [UIView animateWithDuration:0.25 animations:^{
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
                
                //显示暂停按钮
                self.pauseBtn.hidden = NO;
                //用户操作的动画执行完,恢复自动处理
                self.isAnimating = NO;
                self.continueBtn.enabled = YES;
                self.finishBtn.enabled = YES;
            }];
        }
            break;
            
        default:
            break;
    }
}


//点击轨迹按钮
- (IBAction)clickTrackBtn:(id)sender {

    
    [self presentViewController:self.trackVc animated:YES completion:nil];
}


#pragma mark - MPMediaPickerControllerDelegate

//选中某个曲目后调用
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{

    //创建播放器  applicationMusicPlayer当应用退出后,应用播放的系统音乐会保留应用的设置(播放的进度&音量等等状态),如果选择systemMusicPlayer就不会保留应用中播放的状态
    MPMusicPlayerController *player = [MPMusicPlayerController applicationMusicPlayer];
    //设置播放序列
    [player setQueueWithItemCollection:mediaItemCollection];
    //执行播放
    [player play];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击取消后调用
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{

    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
